package com.situ.dao;

import java.util.List;

import com.situ.entily.User;
import com.situ.util.DBUtil;

public class UserDao {

	
	public List<User> selectByUsernmae(String username) {
		String sql = "select * from user where user_name=?";
		return DBUtil.executeDQL(sql, User.class, username);
	}
	public List<User> selectByUsernmae(String username,Integer id) {
		String sql = "select * from user where user_name=? and user_id<>?";
		return DBUtil.executeDQL(sql, User.class, username,id);
	}

	public List<User> all(int index, Integer limit, String keyword) {
		String sql;
		if (keyword == null || "".equals(keyword)) {
			sql = "select * from user limit ?,?";
		} else {
			sql = "select * from user where instr(user_name,'" + keyword + "')>0 limit ?,?";
		}
		return DBUtil.executeDQL(sql, User.class, index, limit);
	}

	public Long count(String keyword) {
		String sql = "select count(1) from user ";
		if (keyword != null && !"".equals(keyword)) {
			sql += " where instr(user_name,'" + keyword + "')>0 ";
		}
		return DBUtil.executeCount(sql);
	}

	public User selectById(Integer id) {
		String sql = "select * from user where user_id=?";

		List<User> list = DBUtil.executeDQL(sql, User.class, id);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public void insert(User u) {
		String sql = "insert into user(user_name,user_password) value(?,?)";
		DBUtil.executeDML(sql, u.getUserName(),u.getUserPassword());
		
	}

	public void update(User u) {
		String sql = "update user set user_name=?,user_password=? where user_id=?";
		DBUtil.executeDML(sql, u.getUserName(),u.getUserPassword(),u.getUserId());
		
	}

	public void delete(Integer id) {
		String sql = "delete from user where user_id=?";
		DBUtil.executeDML(sql, id);		
		
	}
}
